package com.springBoot.springBootSysCore.modules.repository.system;

import com.springBoot.springBootSysCore.common.repository.IBaseRepository;
import com.springBoot.springBootSysCore.modules.entity.system.SysResource;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
 * 
 * @author timo
 *
 */
public interface SysResourceRepository extends IBaseRepository<SysResource, String>{

    @Query(value = "select count(s) from SysResource s")
    public  int findAllCount();
    /**
     * 根据角色code 查询资源code
     * @param roles
     * @return
     */
    @Query("select s.code from SysResource s left join s.roles as r  where r.code in (?1)")
    public List<String> findCodeByRoles(List<String> roles);
    /**
     * 通过用户所拥有的角色Id集合 查询用户拥有的资源
     * @author PSC
     * @param roleIds
     * @return
     */
    @Query("select distinct t from SysResource t inner join t.roles r with r.id in (?1)")
    public List<SysResource> getByRoleIds(Set<String> roleIds);
    /**
     * 查询所有父级菜单
     * @return
     */
    @Query("from SysResource where parentResource is null order by createDate asc")
    public List<SysResource> findAllOfParent();
    /**
     * 根据角色id查询资源id集合
     * @param roleId
     * @return
     */
    @Query("select res.id from SysResource res left join res.roles as role where role.id = ?1")
    public List<String> findResourceForRole(String roleId);
    /**
     * 根据所有父级id查询资源--删除时验证
     * @param parentIds
     * @return
     */
    @Query("from SysResource where parentResource.id in (?1)")
    public  List<SysResource>  findByParentResources(List<String> parentIds);
    /**
     * 根据id删除 资源对象
     * @param asList
     */
    @Modifying
    @Query("delete from SysResource where id in (?1)")
    public void deleteResourcesById(List<String> asList);
    /**
     * 根据code查询资源
     * @author PSC
     * @param code
     * @return
     */
    @Query("from SysResource where code = ?1")
    public SysResource findByCode(String code);
    /**
     * 根据Id集合查询资源
     * @author PSC
     * @param resourceIds
     * @return
     */
    @Query("from SysResource where id in (?1)")
    public List<SysResource> findByIds(List<String> resourceIds);

    /**
     * 根据父级id查询
     * @param id
     * @return
     */
    public List<SysResource> findByParentResourceId(String id);

}
